package com.admin.business.voteRecommend;

import com.admin.business.voteEssentialInformation.VoteEssentialInformationDao;
import com.admin.business.voteOwnerMeeting.VoteOwnerMeetingDao;
import com.admin.business.voteUser.VoteUserDao;
import com.admin.business.voteVotingSettings.VoteVotingSettingsDao;
import com.tiangong.framework.annotation.OpenApi;
import com.tiangong.framework.bo.CDO;
import com.tiangong.framework.bo.Return;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 推举记录表
 * @create: 2022-07-08 19:04:49
 **/
@Service("voteRecommend")
@Slf4j
public class VoteRecommendService {

    @Resource
    VoteRecommendDao voteRecommendDao;


    @Resource
    VoteUserDao voteUserDao;

    @Resource
    VoteOwnerMeetingDao voteOwnerMeetingDao;

    @Resource
    VoteEssentialInformationDao voteEssentialInformationDao;


    @Resource
    VoteVotingSettingsDao voteVotingSettingsDao;

    @OpenApi
    public Return getVoteTotalDataVolume(CDO cdoRequest) {
        Long id = cdoRequest.getLong("essential_information_id");

        CDO voteEssentialInformationById = voteEssentialInformationDao.getVoteEssentialInformationById(Math.toIntExact(id));
        if (voteEssentialInformationById == null) {
            return Return.valueOf(-2, "未查询当前会议");
        }
//
//        获取总人员 和投票票数
        List<CDO> ratioRight = new ArrayList<>();//表

//        ====== 业主端需要附议的面积

        Long essential_information_id = voteEssentialInformationById.getLong("id");//id
        String household_total = voteEssentialInformationById.getString("household_total");//总户数
        String community_province = voteEssentialInformationById.getString("community_province");//省

        Long community_areass = voteEssentialInformationById.getLong("community_areass");

        Long vote_steps = Long.valueOf(1);

        Double ownerMeetingTotal = 0.00;
        CDO totalInfo = new CDO();
        totalInfo.set("essential_information_id", essential_information_id);
        totalInfo.set("audit_status", 0);
        totalInfo.set("state", 0);
        List<CDO> voteOwnerMeetingList = voteUserDao.getVoteUserList(totalInfo);
        ownerMeetingTotal = Double.valueOf(voteOwnerMeetingList.size());//人数总

        CDO recom = new CDO();
        recom.set("essential_information_id", essential_information_id);
        recom.set("state", 0);
        List<CDO> voteRecommendList = voteRecommendDao.getVoteRecommendList(recom);//  投票总票数

        CDO esInfo = new CDO();
        esInfo.set("essential_information_id", essential_information_id);
        esInfo.set("reconsideration_status", 1);
        esInfo.set("state", 0);
        List<CDO> voteUserList = voteUserDao.getVoteUserList(esInfo);//从列表删除人员等于零
        if (voteUserList.size() > 0) {
            CDO voteBuildingArea = voteUserDao.getVoteBuildingArea(esInfo);

            Double userTotal = 0.00;
            userTotal = Double.valueOf(voteUserList.size());//人数总
            Double recomTotal = 0.00;
            recomTotal = Double.valueOf(voteRecommendList.size());//人数总

            Double all = null;
//        获取系统设置的最低人数
            CDO settings = new CDO();
            Double reconsideration_households = 0.00;
            Double reconsideration_area = 0.00;


            Integer reconsideration_households_state = 0;//人数
            Integer reconsideration_area_state = 0;//面积

            String addressName = null;
//        走遍字词表获取当前占
            if (community_province != null) {
                CDO voteAddressInformationProvince = voteEssentialInformationDao.getVoteAddressInformationProvince(community_province);
                addressName = voteAddressInformationProvince.getString("name");

                vote_steps = cdoRequest.getLong("vote_steps");
                if (vote_steps == 1) {
                    settings.set("essential_information_id", 0);
                } else {
                    settings.set("essential_information_id", essential_information_id);
                }
                settings.set("community_province", addressName);
                CDO voteVotingSettings = voteVotingSettingsDao.getVoteVotingSettings(settings);
                if (voteVotingSettings != null) {
                    reconsideration_households_state = voteVotingSettings.getInteger("reconsideration_households_state");//附议蝙蝠之比
                    reconsideration_households = voteVotingSettings.getDouble("reconsideration_households");//人数

                    reconsideration_area_state = voteVotingSettings.getInteger("reconsideration_area_state");//需要附议面积：户数/百分比
                    reconsideration_area = voteVotingSettings.getDouble("reconsideration_area");//面积
                }

            }

//        === 业主端会议同意人数

            CDO householdCdo = new CDO();
            householdCdo.set("name", "人员认证");
            householdCdo.set("value", voteUserList.size());//人员list
            if (reconsideration_households_state == 0) {
                all = Double.valueOf(reconsideration_households);//总人数
            } else {
                all = Double.valueOf(household_total) * (reconsideration_households)*0.01;//总人数
            }
            CDO ratio1 = this.getVoteNumber(all, userTotal, householdCdo);

            ratioRight.add(ratio1);
//            ============


            CDO userSum = new CDO();
            userSum.set("name", "需要收集附议");
            userSum.set("value", voteUserList.size());//人员list
            if (reconsideration_households_state == 0) {
                all = Double.valueOf(reconsideration_households);//总人数
            } else {
                all = Double.valueOf(household_total) * (reconsideration_households) *0.01 ;//总人数
            }
            CDO ratio2 = this.getVoteNumber(all, userTotal, userSum);

            ratioRight.add(ratio2);

            if (voteUserList.size() > 0) {
                CDO voteRecommendCdo = new CDO();
                voteRecommendCdo.set("name", "人员投票");
                voteRecommendCdo.set("value", recomTotal);//投票信息
                all = userTotal + userTotal;//人员投票一遍
                CDO ratio3 = this.getVoteNumber(all, recomTotal, voteRecommendCdo);
                ratioRight.add(ratio3);
//            =====
                CDO ownerMeeting = new CDO();
                ownerMeeting.set("name", "会议同意");
                ownerMeeting.set("value", voteUserList.size());
                if (reconsideration_households_state == 0) {
                    all = Double.valueOf(reconsideration_households);// 附议就不代表投票同意了
                } else {
                    all = Double.valueOf(household_total) * (reconsideration_households * 100)*0.01;//总人数
                }
                CDO ratio4 = this.getVoteNumber(all, ownerMeetingTotal, ownerMeeting);
                ratioRight.add(ratio4);
            }

            Double moneySum = voteBuildingArea.getDouble("moneySum");
            if (moneySum != null) {
                CDO voteareaCdo = new CDO();
                voteareaCdo.set("name", "系统设置面积");
                if (reconsideration_area_state == 0) {
                    all = Double.valueOf(reconsideration_area);//总人数
                } else {
                    all = Double.valueOf(community_areass) * (reconsideration_area * 100)*0.01;//总人数
                }
                voteareaCdo.set("value", moneySum);//总面积
                CDO ratio5 = this.getVoteNumber(all, moneySum, voteareaCdo);
                ratioRight.add(ratio5);

                //      人员房间面积

                CDO moneySumCdo = new CDO();
                moneySumCdo.set("name", "人员面积");
                moneySumCdo.set("value", moneySum);
                all = Double.valueOf(community_areass);//人员面积 站总面积
                CDO ratio6 = this.getVoteNumber(all, moneySum, moneySumCdo);
                ratioRight.add(ratio6);
            }

        }


        return Return.valueOf(ratioRight);
    }

    public CDO getVoteNumber(Double all, Double selectTotal, CDO cdo) {
        Double ratio = selectTotal / all * 100;
        BigDecimal money = new BigDecimal(ratio);
        ratio = money.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        cdo.set("ratio", ratio.toString() + "%");
        return cdo;
    }


//    点击详情

    @OpenApi
    public Return getVoteRecommendRanking(CDO cdoRequest) {

        CDO voteUser = voteUserDao.getVoteUser(cdoRequest);
        if (voteUser == null) {
            return Return.valueOf(-2, "未获取到信息");
        }
        CDO userCdo = new CDO();
        userCdo.set("essential_information_id", cdoRequest.getString("essential_information_id"));
        List<CDO> voteUserList = voteUserDao.getVoteUserList(userCdo);
        for (CDO vote : voteUserList) {
            Long vote_user_id = vote.getLong("id");
            CDO recomDao = new CDO();
            recomDao.set("vote_user_id", vote_user_id);
            recomDao.set("recommend_state", cdoRequest.getString("recommend_state"));
            List<CDO> voteRecommendList = voteRecommendDao.getVoteRecommendList(recomDao);
            vote.set("voteRecommendList", voteRecommendList);
        }

        return Return.valueOf(voteUserList);


    }

    /**
     * 添加推举记录表,获取id数据
     *
     * @param cdoRequest - id:
     *                   - vote_user_id:被投票用户id
     *                   - essential_information_id:基本数据id
     *                   - vote_user:投票用户
     *                   - user_name:投票人名称
     *                   - user_building_number:投票人楼号
     *                   - recommend_state:推举状态：1、筹备组推举，2、委员推举
     *                   - update_time:更新时间
     *                   - begin_time:创建时间
     *                   - state:删除，0正常1删除
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return addVoteRecommend(CDO cdoRequest) {
        voteRecommendDao.addVoteRecommend(cdoRequest);
        CDO cdoResult = new CDO();
        if (cdoRequest.containsKey("id")) {
            cdoResult.set("id", cdoRequest.get("id"));
        }
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据id，更新推举记录表数据
     *
     * @param cdoRequest - id:
     *                   - vote_user_id:被投票用户id
     *                   - essential_information_id:基本数据id
     *                   - vote_user:投票用户
     *                   - user_name:投票人名称
     *                   - user_building_number:投票人楼号
     *                   - recommend_state:推举状态：1、筹备组推举，2、委员推举
     *                   - update_time:更新时间
     *                   - begin_time:创建时间
     *                   - state:删除，0正常1删除
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return updateVoteRecommendById(CDO cdoRequest) {
        int affectedRows = voteRecommendDao.updateVoteRecommendById(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据id，删除推举记录表数据
     *
     * @param cdoRequest - id:
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return deleteVoteRecommendById(CDO cdoRequest) {
        int affectedRows = voteRecommendDao.deleteVoteRecommendById(cdoRequest.getInteger("id"));
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据条件，删除推举记录表数据
     *
     * @param cdoRequest
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return deleteVoteRecommend(CDO cdoRequest) {
        int affectedRows = voteRecommendDao.deleteVoteRecommend(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据ID，获取推举记录表数据
     *
     * @param cdoRequest - id:
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return getVoteRecommendById(CDO cdoRequest) {
        CDO cdoMapperResult = voteRecommendDao.getVoteRecommendById(cdoRequest.getInteger("id"));
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 根据混合条件，获取推举记录表数据
     *
     * @param cdoRequest - id:
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return getVoteRecommend(CDO cdoRequest) {
        CDO cdoMapperResult = voteRecommendDao.getVoteRecommend(cdoRequest);
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 根据非空条件，获取推举记录表数据列表
     *
     * @param cdoRequest
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return getVoteRecommendList(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 100;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = voteRecommendDao.getVoteRecommendList(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
//        int total = voteRecommendDao.getVoteRecommendListCount(cdoRequest);
//        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据混合条件，获取推举记录表数据列表
     *
     * @param cdoRequest - id:
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return getVoteRecommendListByCondition(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = voteRecommendDao.getVoteRecommendListByCondition(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
//        int total = voteRecommendDao.getVoteRecommendListByConditionCount(cdoRequest);
//        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }
}
